package de.au.fi.imp.reader;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebReader {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	WebDriver driver;

	protected WebElement element;
	
	protected void getFirefoxDriver() {
		this.driver = new FirefoxDriver(new FirefoxBinary(new
				 File("/Applications/Firefox.app/Contents/MacOS/firefox")),null);
	}
	

	protected WebElement setElementTo(By by) {
		try {
			element = driver.findElement(by);
		} catch (Exception e) {
			log.error("Could not find element {} {}",by.toString(), e.getMessage());
			element = null;
		}
		return element;

	}
	
	protected String getText()
	{
		if(element == null)
			return null;
		return element.getText();
	}
	
	protected String logElementText(String label) {
		try {
			String text = getText();
			log.debug("{}={}", label, text);
			return text;
		} catch (Exception e) {
			log.error("Could not find " + label);
		}
		return null;
	}

	protected void click() {
		element.click();
		wait(2);
	}

	protected void wait(int seconds) {
		if (seconds == 0)
			return;
		try {
			Thread.sleep(seconds * 1000);
		} catch (Exception e) {
		}
	}

	public void destroy() {
		if (driver != null)
			driver.quit();
		this.driver = null;
		this.element = null;
	}

}
